Skip to content

jax: register FitImaging, Tracer, DatasetModel as pytrees on use_jax path - #445

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/fit-imaging-pytree
Apr 19, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/fit-imaging-pytree

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Path A for the fit-imaging-pytree task (issue #444). AnalysisImaging.fit_from now registers FitImaging, Tracer, and DatasetModel as JAX pytree nodes on the first call when _use_jax is true. With this in place, jax.jit(analysis.fit_from)(instance) returns a real FitImaging with jax.Array leaves — previously it raised TypeError: ... is not a valid JAX type at the JIT output boundary.

dataset, adapt_images, settings, and cosmology are pushed through aux_data (per-analysis constants, closed over the JIT boundary) via the no_flatten option on the generic register_instance_pytree helper added in the companion PyAutoArray PR. Only tracer, dataset_model, and their nested autoarray wrappers flow through as dynamic children.

CLAUDE.md is updated: the "autoarray types cannot be returned from jax.jit" paragraph now documents two coexisting patterns (the existing if xp is np: raw-array guard, and the new pytree-registered wrapper return).

PoC: autolens_workspace_test/scripts/jax_likelihood_functions/imaging/mge_pytree.py — jax.jit(analysis.fit_from)(instance).log_likelihood matches the NumPy path to ~1e-8 relative error for the MGE parametric lens + MGE source model.

Depends on: PyAutoLabs/PyAutoArray#288

Issue: #444

API Changes

One private addition, no public removals/renames:

  • New static method AnalysisImaging._register_fit_imaging_pytrees() — registers FitImaging, Tracer, DatasetModel as JAX pytree nodes. Called from fit_from when self._use_jax is true. Idempotent.
  • Behavioural change: jax.jit(analysis.fit_from)(instance) now succeeds on the JAX path. Previously it raised at the JIT output boundary.

No removals, renames, or public signature changes.

See full details below.

Test Plan

  • pytest test_autolens/ — 246 passed
  • pytest test_autoarray/ — 736 passed
  • pytest test_autogalaxy/ — 836 passed
  • pytest test_autofit/ — 1232 passed
  • MGE PoC: autolens_workspace_test/scripts/jax_likelihood_functions/imaging/mge_pytree.py returns -86629.349379496 under jax.jit, matching the NumPy scalar to ~1e-8 rel
Full API Changes (for automation & release notes)

Added

  • autolens.imaging.model.analysis.AnalysisImaging._register_fit_imaging_pytrees() (staticmethod) — registers FitImaging, Tracer, and DatasetModel as JAX pytree nodes via the generic autoarray.abstract_ndarray.register_instance_pytree helper. dataset, adapt_images, settings, and cosmology ride as aux_data. Called once at the top of fit_from when self._use_jax is true. Idempotent.

Changed Behaviour

  • AnalysisImaging.fit_from(instance) — when constructed with use_jax=True, the returned FitImaging can now be returned from a jax.jit-compiled function. Previously jax.jit(analysis.fit_from)(instance) raised TypeError: ... FitImaging ... is not a valid JAX type.

Migration

None required. Pure NumPy callers see no change. JAX callers who were previously working around the output-boundary error can now return FitImaging directly from jit'd code.

🤖 Generated with Claude Code

…path

AnalysisImaging.fit_from now calls _register_fit_imaging_pytrees on the
JAX path, so jax.jit(analysis.fit_from)(instance) returns a real FitImaging
with jax.Array leaves instead of raising TypeError at the JIT output
boundary. dataset, adapt_images, settings, and cosmology ride as aux_data
(per-analysis constants); tracer, dataset_model, and nested autoarray
wrappers flow through as dynamic children.

Updates CLAUDE.md to document the pytree-wrapper-return pattern alongside
the existing if-xp-is-np raw-array pattern.

Issue: #444
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label Apr 19, 2026
@Jammy2211
Jammy2211 merged commit 9864c85 into main Apr 19, 2026
5 checks passed
@Jammy2211

Copy link
Copy Markdown
Collaborator Author

Workspace PR: PyAutoLabs/autolens_workspace_test#32

@Jammy2211
Jammy2211 deleted the feature/fit-imaging-pytree branch April 19, 2026 12:55
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant